function LoanUserCk(){
    $("#year").val($("#hidyear").val());
	this.form=this.InitForm();
	this.table=this.InitTable();
	this.InitOrg();
}

LoanUserCk.prototype.InitForm=function(){
	var form;
	layui.use('form', function(){
       form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
       form.on('select(year)', function (data) { 
    	   vLoanUserCk.searchtable();
       });
       
	   form.render();
	});  
	return form;
}

LoanUserCk.prototype.InitOrg = function() {
	var comboboxm=$("#orgid").ligerComboBox({
		valueField : 'orgid',
		textField : 'orgname',
		selectBoxWidth : 250,
		selectBoxHeight:250,
		treeLeafOnly:false,
		valueFieldID:"hid",
		tree: {url : '../common/get_org_list?orgtype=0',
			checkbox: false, 
			nodeWidth:150,
			ajaxType: 'get', 
			idFieldName: 'orgid',
			textFieldName:"orgname",
			idField:"orgid",
        	parentIDField:"orgparentid"
		},
		onSelected : function(orgid, orgname) {// id为空值，name为选择的值
			$("#hidorgid").val(orgid);
			vLoanUserCk.searchtable();
		},
		width : 250
	});
	return comboboxm;
}

LoanUserCk.prototype.InitTable = function() {
	var table;
	var _this=this;
	layui.use('table', function() {
	    table = layui.table;
		table.render({
			elem : '#list'
			,height : $(window).height() - 95
			,url : 'get_list_ck' // 数据接口
			,page : true // 开启分页
			,limit : 40
			,id: 'vtdpuserlist'
		    ,limits:[20,40,80,100]
			,method:"post"
			,cols : [ [ // 表头
			           //{type:'numbers'}
			            //,{field : 'rowNum',title : '序号',width : 80,align : 'center'}
			            {field : 'realname',title : '姓名',width : 130,align : 'center',fixed: 'left',templet: '#realnameTpl'}
			           //, {field : 'sex',title : '性别',width : 80,align : 'center'}
			           , {field : 'cardno',title : '身份证号',width : 180,align : 'center',event:'glxs'}
			           , {field : 'orgname',title : '实际工作单位',width : 200,align : 'center',event:'glxs'}
			           , {field : 'rsgxsorgname',title : '人事关系所在单位',width : 200,align : 'center',event:'glxs'}
			           , {field : 'loanorgname',title : '借调部门',width : 200,align : 'center',event:'glxs'}
			           //, {field : 'userno',title : '警号',width : 150,align : 'center'}
			           , {field : 'status',title : '状态',width : 100,align : 'center',event:'glxs'}
			           , {field : 'begintime',title : '借调开始时间',width : 120,align : 'center',event:'glxs'}
			           , {field : 'endtime',title : '借调结束时间',width : 120,align : 'center',event:'glxs'}
			           , {field : 'byrealname',title : '填报人',width : 100,align : 'center',event:'glxs'}
			           , {field : 'createtime',title : '时间',width : 180,align : 'center',event:'glxs'}
			           , {field : 'loandes',title : '备注',width : 150,align : 'center',event:'glxs'}
			           , {title : '操作',width : 120,align : 'center',toolbar : '#operate'}
		            ] ]
		    ,request:{
				type:'post'
			}
			,where:{
				year: $("#year").val(),
				orgid:$("#hidorgid").val(),
				realname:$("#realname").val()
				}
		    ,done: function(res, page, count){
		    	
		    	var dataarr=res.data;
		    	$("[data-field='begintime']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			if(text!=""&&text!=null){
		    				var begin = (new Date(Date.parse(text.replace(/-/g,"/")))).format("yyyy-MM-dd");
			    			$(this).text(begin);
		    			}
		    		}
               })  
               $("[data-field='endtime']").children().each(function(index){ 
		    		if(index>0){
		    			var text=$(this).text();
		    			if(text!=""&&text!=null){
		    				var begin = (new Date(Date.parse(text.replace(/-/g,"/")))).format("yyyy-MM-dd");
			    			$(this).text(begin);
		    			}
		    		}
              })  
              
              $("[data-field='status']").children().each(function(index){ 
           	   var text=$(this).text();
           	   switch(text){
           	   case "0":
           		  $(this).text("填报");
           		   break;
           	   case "1":
           		  $(this).text("提交");
          		   break;
           	   case "2":
           		  $(this).text("归档");
         		       break;
           	   case "3":
           		  $(this).text("驳回");
         		       break;
           	  case "4":
           		  $(this).text("接收");
         		       break;
           	  case "5":
          		  $(this).text("");
        		       break;
           	  case "6":
         		       $(this).text("申请会原单位");
       		       break;
           	 case "7":
       		       $(this).text("回原单位归档");
     		       break;
           	   }
             })  
              
              $("[data-field='sex']").children().each(function(index){ 
           	   var text=$(this).text();
           	   if(text=="0"){
           		   $(this).text('男')
           	   }
           	   else if(text=="1"){
           		   $(this).text('女')
           	   }
             })  
               
		    }
		    
		});

		
		 //监听工具条
	    table.on('tool(list)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	        layer.msg('ID：'+ data.id + ' 的查看操作');
	      } else if(obj.event === 'del'){
	        top.mylayui.confirm('确定要删除吗？',{icon: 0}, function(index){
	        	vLoanUserCk.deleteentity(obj.data.loanid);
	        });
	      } else if(obj.event === 'tijiao'){
	    	  vLoanUser.updatestatus(obj.data.userid,obj.data.loanid,0);
	      }
	      else if(obj.event === 'edit'){
	    	  vLoanUserCk.openadd(obj.data.loanid);
	      }
	    });

	    
	});
	
	return table;
}

LoanUserCk.prototype.searchtable=function(){
	this.table.reload('vtdpuserlist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    ,where: {  
	    	year: $("#year").val(),
			orgid:$("#hidorgid").val(),
			realname:$("#realname").val()
	    }  
	}); 
}

LoanUserCk.prototype.openadd=function(loanid){
	var title="填写";
	if(loanid>0){
		title="编辑";
	}
	top.mylayui.open({
         type: 2,
         title: title,
         shadeClose: true,
         shade: 0.5,
         area: ['620px', '400px'],
         content: 'loanuser/loan_tb_edit?loanid='+loanid
       }); 
	var iframeele=top.$(".layui-layer-content").find('iframe');
	iframeele.css({"height":$(iframeele).height()-10});
}

LoanUserCk.prototype.deleteentity=function(loanid){
	$.ajax({
		type:"POST",
		url:"delete_loanuser",
		data:{loanid:loanid},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				//vOrg.searchtable();
				document.location.reload();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}


var vLoanUserCk;
$(function () {
	vLoanUserCk = new LoanUserCk();
})


function CloseDialog(result,msg){
	if(result>0){
		top.mylayui.msg('操作成功', {icon: 1});
		document.formedit.reset();
		document.location.reload();
	}
	else{
		if(msg){
			top.mylayui.msg(msg, {icon: 0});
		}else{
			top.mylayui.msg('操作失败', {icon: 0});
		}
	}
}